package eclipse.util.io;
import java.io.*;
import java.net.*;
import eclipse.ErrorManager;
/**
 * IOTools is a class of static methods that can be used to modify any form of files on the system.
 */
public class IOTools 
{
	/**
	 * Reads an entire file as a single String.
	 * @param sFile String path to the file.
	 */
	public static String readAsString(String sFile)
	{
		try
		{
	        StringBuffer fileData = new StringBuffer(1000);
	        BufferedReader reader = new BufferedReader(new FileReader(sFile));
	        char[] buf = new char[1024];
	        int numRead=0;
	        while((numRead=reader.read(buf)) != -1)
	        {
	            String readData = String.valueOf(buf, 0, numRead);
	            fileData.append(readData);
	            buf = new char[1024];
	        }
	        reader.close();
	        return fileData.toString();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return "";
		}
	}
	/**
	 * Reads an entire file as an array of bytes.
	 * @param sFile String path pointing to the file.
	 */
	 public static byte[] readAsBytes(String sFile)
	 {		 		 
		 byte[] bytes = new byte[0];
		 try
		 {
			File file = new File(sFile);
			long length = file.length();
			bytes = new byte[(int)length];							
	        InputStream is = new FileInputStream(file);	    	      	            	
	        int offset = 0;
	        int numRead = 0;
	        
	        if(length > Integer.MAX_VALUE)	        
	        	throw new IOException("File is too large!");	        	           
	        
	        while (offset < bytes.length
	               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) 
	        {
	            offset += numRead;
	        }
	    
	        // Ensure all the bytes have been read in
	        if (offset < bytes.length) 
	        {
	            throw new IOException("Could not completely read file "+file.getName());
	        }
	    
	        // Close the input stream and return bytes
	        is.close();
		 }
		 catch(Exception e)
		 {
			 eclipse.ErrorManager.handleError(e);
		 }	     
		 return bytes;
	}
	 /**
	  * Writes an array of bytes to a file.
	  * @param sFile String path to the file.
	  * @param byteArray Array of bytes to be written to the file.
	  */
	public static void writeBytesToFile(String sFile, byte[] byteArray)
	{
		try
		{
			FileOutputStream fos = new FileOutputStream(sFile);
			fos.write(byteArray);
			fos.flush();
			fos.close();			
		}
		catch(Exception e)
		{
			eclipse.ErrorManager.handleError(e);
		}
	}
	/**
	 * Creates a new file. If the file already exists, it deletes it first.
	 * @param sFile String path to the file. 
	 */
	public static void makeFile(String sFile)
	{
		if(!fileExists(sFile))
			killFile(sFile);
		new DataFile(sFile).writeString("");			
	 }
	public static boolean dirExists(String sDir)
	{
		return fileExists(sDir);
	}
	/**
	 * Creates a new directory.
	 * @param sDir
	 */
	public static void makeDir(String sDir)
	{
		new File(sDir).mkdir();
	}
	/**
	 * Returns a list of Files in the specified directory.
	 * @param directory String path for a specific directory.
	 */
	public static File[] getFiles(String directory)
	{
		 File folder = new File(directory);
		 File[] listOfFiles = folder.listFiles();
		 
		 return listOfFiles;   		  
	}	
	/**
	 * Deletes a specific file.
	 * @param sFile String path to a file.
	 */
	public static void killFile(String sFile)
	{
		new File(sFile).delete();
	}
	/**
	 * Returns true if the file exists, otherwise it returns false.
	 * @param sFile String path to the specific file.
	 */
	public static boolean fileExists(String sFile)
	{
		return new File(sFile).exists();
	}
	public static void download(String sAddress, String sFile)
	{
		try
		{
			URL url = new URL(sAddress);
			download(url, sFile);
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	public static void download(URL uAddress, String file)
	{
		try
		{
			OutputStream 	out = new BufferedOutputStream(new FileOutputStream(file));
			InputStream		in  = uAddress.openStream();
			byte[] buffer = new byte[1024];
			int c;
			while((c = in.read(buffer)) != -1)		
				out.write(buffer, 0, c);		
			in.close();
			out.close();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
}